﻿<view:AViewPage 
    x:Class="OneBusAway.WP7.View.DetailsPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:navigation="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:m="clr-namespace:Microsoft.Phone.Controls.Maps;assembly=Microsoft.Phone.Controls.Maps"
    xmlns:data="clr-namespace:OneBusAway.WP7.ViewModel;assembly=OneBusAway.WP7.ViewModel" 
    xmlns:view="clr-namespace:OneBusAway.WP7.View"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:shapes="clr-namespace:System.Windows.Shapes;assembly=System.Windows"
    xmlns:tilt="clr-namespace:ControlTiltEffect"
    tilt:TiltEffect.IsTiltEnabled="True"
    SupportedOrientations="Portrait"
    mc:Ignorable="d" d:DesignHeight="728" d:DesignWidth="480"
    shell:SystemTray.IsVisible="True"
    >

    <view:AViewPage.Resources>

        <data:RouteDetailsVM x:Key="ViewModel" />

        <view:DateTimeDeltaConverter x:Key="DateTimeDeltaConverter" />
        <view:DateTimeConverter x:Key="DateTimeConverter" />
        <view:VisibilityConverter x:Key="VisibilityConverter" />
        <view:DelayColorConverter x:Key="DelayColorConverter" />
        <view:PolylineConverter x:Key="PolylineConverter" />
        <view:PushpinScaleConverter x:Key="PushpinScaleConverter" />
        
        <!-- TextBox styles -->

        <DataTemplate x:Key="RouteTemplate">
            <Grid Margin="5">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="80" />
                    <ColumnDefinition Width="250" />
                    <ColumnDefinition Width="150" />
                </Grid.ColumnDefinitions>

                <Grid.RowDefinitions>
                    <RowDefinition Height="1*" />
                    <RowDefinition Height="1*" />
                </Grid.RowDefinitions>

                <TextBlock 
                    Grid.Column="0" 
                    Grid.Row="0" 
                    Grid.RowSpan="2" 
                    Text="{Binding routeShortName}" 
                    FontSize="{StaticResource PhoneFontSizeLarge}" 
                    Foreground="{StaticResource OBAForegroundBrush}"
                    Margin="5" 
                    />
                <TextBlock 
                    Grid.Column="1" 
                    Grid.ColumnSpan="2" 
                    Grid.Row="0" 
                    Foreground="{StaticResource OBAForegroundBrush}"                    
                    Text="{Binding tripHeadsign}" 
                    />
                <TextBlock 
                    Grid.Column="1" 
                    Grid.Row="1" 
                    Foreground="{StaticResource OBASubtleBrush}" 
                    FontSize="{StaticResource PhoneFontSizeSmall}" 
                    Text="{Binding Path=nextKnownArrival, Converter={StaticResource DateTimeConverter}}" 
                    />
                <TextBlock 
                    Grid.Column="2" 
                    Grid.Row="0" 
                    Grid.RowSpan="2" 
                    Style="{StaticResource PhoneTextLargeStyle}" 
                    HorizontalAlignment="Center" 
                    VerticalAlignment="Center" 
                    Text="{Binding Path=nextKnownArrival, Converter={StaticResource DateTimeDeltaConverter}}" 
                    Foreground="{Binding Converter={StaticResource DelayColorConverter}}"
                    />

            </Grid>
        </DataTemplate>

        <Style x:Key="BusStopLargePushpinStyle" TargetType="m:Pushpin">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Button 
                            Width="50" Height="50"
                            HorizontalContentAlignment="Stretch" 
                            VerticalContentAlignment="Stretch" 
                            BorderThickness="0" 
                            Padding="0" 
                            Click="BusStopPushpin_Click"
                            Tag="{TemplateBinding Tag}"
                            >
                            <Image Source="/Bus_Icon.png" Margin="-3"/>
                        </Button>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    </view:AViewPage.Resources>

    <Grid x:Name="LayoutRoot" Background="{StaticResource OBAPrimaryBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="70"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="200"/>
        </Grid.RowDefinitions>

        <Grid Grid.Row="0" x:Name="TitleGrid">
            <StackPanel Orientation="Horizontal">
                <TextBlock VerticalAlignment="Center" x:Name="RouteNumber" Foreground="{StaticResource OBAForegroundBrush}" Text="{Binding Path=CurrentViewState.CurrentRoute.shortName, Source={StaticResource ViewModel}}" FontSize="48"  Margin="15" />

                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                    </Grid.RowDefinitions>

                    <TextBlock Grid.Row="0" x:Name="RouteName" Foreground="{StaticResource OBAForegroundBrush}" Text="{Binding Path=CurrentViewState.CurrentRouteDirection.name, Source={StaticResource ViewModel}}" FontSize="{StaticResource PhoneFontSizeMedium}"/>
                    <TextBlock Grid.Row="1" x:Name="RouteInfo" Foreground="{StaticResource OBAForegroundBrush}" Text="{Binding Path=CurrentViewState.CurrentStop.name, Source={StaticResource ViewModel}}" FontSize="{StaticResource PhoneFontSizeMedium}"/>
                </Grid>
            </StackPanel>
        </Grid>

        <Grid Grid.Row="1" x:Name="ContentGrid">
            <m:Map x:Name="DetailsMap" CopyrightVisibility="Collapsed" LogoVisibility="Collapsed" ScaleVisibility="Collapsed" CredentialsProvider="{StaticResource MapCredentials}"
                   Center="{Binding Path=LocationTracker.CurrentLocationSafe, Source={StaticResource ViewModel}}"  ZoomLevel="10"
                    MapZoom="DetailsMap_MapZoom">
                <m:MapLayer x:Name="RouteLineLayer">
                    <m:MapItemsControl x:Name="RouteLineControl" ItemsSource="{Binding Path=CurrentViewState.CurrentRouteDirection, Source={StaticResource ViewModel}, Converter={StaticResource PolylineConverter}}">
                        <m:MapItemsControl.ItemTemplate>
                            <DataTemplate>
                                <m:MapPolyline 
                                Locations="{Binding}" 
                                StrokeThickness="5"
                                Stroke="{StaticResource OBAAccentBrush}"
                                />
                            </DataTemplate>
                        </m:MapItemsControl.ItemTemplate>
                    </m:MapItemsControl>
                </m:MapLayer>
                <m:MapLayer x:Name="BusTripsLayer">
                    <m:MapItemsControl x:Name="BusTripsItemsControl" ItemsSource="{Binding Path=TripDetailsForArrivals, Source={StaticResource ViewModel}}">
                        <m:MapItemsControl.ItemTemplate>
                            <DataTemplate>
                                <m:Pushpin Location="{Binding location}" PositionOrigin="Center" Style="{StaticResource BusTripPushpinStyle}"  />
                            </DataTemplate>
                        </m:MapItemsControl.ItemTemplate>
                    </m:MapItemsControl>
                </m:MapLayer>
                <m:MapLayer x:Name="BusStopsLayer" Visibility="Collapsed">
                    <m:MapItemsControl x:Name="BusStopItemsControl" ItemsSource="{Binding Path=CurrentViewState.CurrentRouteDirection.stops, Source={StaticResource ViewModel}}">
                        <m:MapItemsControl.ItemTemplate>
                            <DataTemplate>
                                <m:Pushpin 
                                    Location="{Binding location}" 
                                    PositionOrigin="Center" 
                                    Tag="{Binding Path=id}" 
                                    Style="{StaticResource BusStopLargePushpinStyle}" 
                                    />
                                <!-- 
                                    TODO: Enable this once I get it working
                                    RenderTransform="{Binding Path=ZoomLevel, ElementName=DetailsMap, Converter={StaticResource PushpinScaleConverter}}"
                                -->
                            </DataTemplate>
                        </m:MapItemsControl.ItemTemplate>
                    </m:MapItemsControl>
                </m:MapLayer>
                <m:MapLayer x:Name="SelectedStopLayer">
                    <m:Pushpin Location="{Binding Path=CurrentViewState.CurrentStop.location, Source={StaticResource ViewModel}}" PositionOrigin="BottomLeft">
                        <Image Source="/Bus_Icon.png" Width="25" Height="25" />
                    </m:Pushpin>
                </m:MapLayer>
                <m:MapLayer x:Name="MyLocationLayer">
                    <m:Pushpin Location="{Binding Path=LocationTracker.CurrentLocationSafe, Source={StaticResource ViewModel}}" PositionOrigin="Center" Style="{StaticResource MyLocationPushpinStyle}" />
                </m:MapLayer>
            </m:Map>
        </Grid>
        <Grid Grid.Row="2" x:Name="StopGrid">
            <ListBox 
                x:Name="ArrivalsListBox" 
                ItemsSource="{Binding Path=ArrivalsForStop, Source={StaticResource ViewModel}}" 
                ItemTemplate="{StaticResource RouteTemplate}" 
                SelectionChanged="ArrivalsListBox_SelectionChanged"
                />
        </Grid>
        
        <ProgressBar 
            Visibility="{Binding Path=Loading, Source={StaticResource ViewModel}, Converter={StaticResource VisibilityConverter}}"
            IsIndeterminate="True" 
            Height="4" 
            HorizontalAlignment="Left" 
            Name="ProgressBar" 
            VerticalAlignment="Top" 
            Width="480" 
            Style="{StaticResource PerformanceProgressBar}"
            Foreground="{StaticResource OBALightBrush}"
            />

    </Grid>

    <navigation:PhoneApplicationPage.ApplicationBar >
        <shell:ApplicationBar ForegroundColor="{StaticResource OBAForegroundColor}" IsVisible="True" IsMenuEnabled="True"  BackgroundColor="{StaticResource OBADarkColor}">
            <shell:ApplicationBarIconButton x:Name="appbar_favorite" IconUri="/Images/appbar.favs.addto.rest.png" Text="Favorite" Click="appbar_favorite_Click" />
            <shell:ApplicationBarIconButton x:Name="appbar_refresh" IconUri="/Images/appbar.refresh.rest.png" Text="Refresh" Click="appbar_refresh_Click" />
        </shell:ApplicationBar>
    </navigation:PhoneApplicationPage.ApplicationBar>
</view:AViewPage>
